/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tukano.odt;

import it.tukano.fun.Tuple2;
import it.tukano.fun.VFunction1;
import it.tukano.lang.StringExt;
import it.tukano.swingdeco.containers.JPanelExt;
import it.tukano.swingdeco.controls.JButtonExt;
import it.tukano.swingdeco.input.CalendarChooser;
import it.tukano.swingdeco.text.DigitsDocument;
import it.tukano.swingdeco.text.JTextFieldExt;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author utente3
 */
public class OdtDateEditor extends OdtFieldEditor {

    /* default serial version uid*/
    private static final long serialVersionUID = 0;
    private final String defaultValue;
    private final JTextFieldExt day, month, year;
    private final JPanel panel;
    private boolean noDay;
    private final JPanelExt box = new JPanelExt(new GridLayout(1, 3));
    
    public OdtDateEditor() {
        this(1, null, null);
    }

    public OdtDateEditor(int size, String label, StringExt defaultValue) {
        this.defaultValue = defaultValue == null ? "" : defaultValue.toString();
        if(label != null) {
            setBorder(BorderFactory.createTitledBorder(label));
        }
        day = new JTextFieldExt(3).withDocument(new DigitsDocument()).alignCenter();
        month = new JTextFieldExt(3).withDocument(new DigitsDocument()).alignCenter();
        year = new JTextFieldExt(6).withDocument(new DigitsDocument()).alignCenter();
        box.add(day);
        box.add(month);
        box.add(year);
        JButtonExt chooser = new JButtonExt(OdtConfiguration.getInstance().getOdtDateEditorIcon());
        chooser.onAction(new Runnable() {

            public void run() {
                OdtDateEditor.this.popupDateChooser();
            }
        });
        panel = new JPanel(new BorderLayout());
        panel.add(box, BorderLayout.CENTER);
        panel.add(chooser, BorderLayout.WEST);
        add(panel);
    }
    
    public void onTextChange(final Runnable task) {
        DocumentListener dl = new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                task.run();
            }

            public void removeUpdate(DocumentEvent e) {
                task.run();
            }

            public void changedUpdate(DocumentEvent e) {
                task.run();
            }
        };
        day.getDocument().addDocumentListener(dl);
        month.getDocument().addDocumentListener(dl);
        year.getDocument().addDocumentListener(dl);
    }

    @Override
    public void setValue(StringExt t) {
        Calendar c = t.toDate(null);
        if(c != null) {
            day.setString(c.get(Calendar.DAY_OF_MONTH));
            month.setString(c.get(Calendar.MONTH) + 1);
            year.setString(c.get(Calendar.YEAR));
        } else {
            day.setString("");
            month.setString("");
            year.setString("");
        }
    }

    @Override
    public Tuple2<StringExt, String> applyEditorValue(OdtField odtField, String xml) {
        StringExt s = new StringExt(day.getText() + "/" + month.getText() + "/" + year.getText());
        return Tuple2.newInstance(s, xml);
    }

    @Override
    public void clearValue() {
        day.setText("");
        month.setText("");
        year.setText("");
    }

    private void popupDateChooser() {
        CalendarChooser.showDialog(this, Calendar.getInstance(), true, new VFunction1<Calendar>() {

            @Override
            protected void doApply(Calendar p) {
                int d = p.get(Calendar.DAY_OF_MONTH);
                int y = p.get(Calendar.YEAR);
                int m = p.get(Calendar.MONTH) + 1;
                day.setText(String.valueOf(d));
                month.setText(String.valueOf(m));
                year.setText(String.valueOf(y));
            }
        });
    }

    /* remove the day field from the editor*/
    public OdtDateEditor noDay() {
        noDay = true;
        day.setVisible(false);
        return this;
    }

}
